home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / admin.php next >
Encoding:
PHP Script  |  2002-06-12  |  15.5 KB  |  615 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > Admin wrapper script
  17. |   > Script written by Matt Mecham
  18. |   > Date started: 1st March 2002
  19. |
  20. +--------------------------------------------------------------------------
  21. */
  22.  
  23. /*-----------------------------------------------
  24.   USER CONFIGURABLE ELEMENTS
  25.  ------------------------------------------------*/
  26.  
  27. // Root path
  28.  
  29. $root_path = "./";
  30.  
  31. // Check IP address to see if they match?
  32. // this may cause problems for users on proxies
  33. // where the IP address changes during a session
  34.  
  35. $check_ip = 1;
  36.  
  37. // Use GZIP content encoding for fast page generation
  38. // in the admin center?
  39.  
  40. $use_gzip = 1;
  41.  
  42.  
  43. /*-----------------------------------------------
  44.   NO USER EDITABLE SECTIONS BELOW
  45.  ------------------------------------------------*/
  46.  
  47. error_reporting  (E_ERROR | E_WARNING | E_PARSE);
  48. set_magic_quotes_runtime(0);
  49.  
  50. if (function_exists('ini_get'))
  51. {
  52.     $safe_switch = ini_get("safe_mode") ? 1 : 0;
  53. }
  54. else
  55. {
  56.     $safe_switch = 1;
  57. }
  58.  
  59. define( 'SAFE_MODE_ON', $safe_switch );
  60.  
  61. if (function_exists("set_time_limit") == 1 and SAFE_MODE_ON == 0)
  62. {
  63.   @set_time_limit(0);
  64. }
  65.  
  66.  
  67. class Debug {
  68.     function startTimer() {
  69.         global $starttime;
  70.         $mtime = microtime ();
  71.         $mtime = explode (' ', $mtime);
  72.         $mtime = $mtime[1] + $mtime[0];
  73.         $starttime = $mtime;
  74.     }
  75.     function endTimer() {
  76.         global $starttime;
  77.         $mtime = microtime ();
  78.         $mtime = explode (' ', $mtime);
  79.         $mtime = $mtime[1] + $mtime[0];
  80.         $endtime = $mtime;
  81.         $totaltime = round (($endtime - $starttime), 5);
  82.         return $totaltime;
  83.     }
  84. }
  85.  
  86. class info {
  87.  
  88.     var $vars    = "";
  89.     var $version = '1.0';
  90.     
  91.     
  92.     function info($INFO)
  93.     {
  94.         //global $INFO;
  95.         $this->vars = $INFO;
  96.     }
  97. }
  98.         
  99.  
  100.  
  101. /*-----------------------------------------------
  102.   Import $INFO
  103.  ------------------------------------------------*/
  104.  
  105. require $root_path."conf_global.php";
  106.  
  107. $ibforums = new info($INFO);
  108.  
  109. $Debug = new Debug;
  110. $Debug->startTimer();
  111.  
  112. /*-----------------------------------------------
  113.   Make sure our data is reset on each invocation
  114.  ------------------------------------------------*/
  115. $MEMBER          = array();
  116. $SESSION_ID      = "";
  117. $SKIN            = "";
  118.  
  119. // Put an end to insane thoughs before they begin
  120. $MEMBER_NAME     = "";
  121. $MEMBER_PASSWORD = "";
  122. $MEMBER_EMAIL    = "";
  123. $UserName        = "";
  124. $PassWord        = "";
  125.  
  126.  
  127. /*-----------------------------------------------
  128.   Load up our classes (compiled into one package)
  129.  ------------------------------------------------*/
  130.  
  131. require $root_path."sources/functions.php";
  132.  
  133. $std     = new FUNC;
  134.  
  135. /*-----------------------------------------------
  136.   Steralize our FORM and GET input
  137.  ------------------------------------------------*/
  138.  
  139. $IN = $std->parse_incoming();
  140.  
  141. $IN['AD_SESS'] = $HTTP_POST_VARS['adsess'] ? $HTTP_POST_VARS['adsess'] : $HTTP_GET_VARS['adsess'];
  142.  
  143. /*-----------------------------------------------
  144.   Import $PAGES and $CATS
  145.  ------------------------------------------------*/
  146.  
  147. require $root_path."sources/Admin/admin_pages.php";
  148.  
  149.  
  150. /*-----------------------------------------------
  151.   Import Skinable elements
  152.  ------------------------------------------------*/
  153.  
  154. require $root_path."sources/Admin/admin_skin.php";
  155.  
  156. $SKIN = new admin_skin();
  157.  
  158. /*-----------------------------------------------
  159.   Import Admin Functions
  160.  ------------------------------------------------*/
  161.  
  162. require $root_path."sources/Admin/admin_functions.php";
  163.  
  164. $ADMIN = new admin_functions();
  165.  
  166. /*-----------------------------------------------
  167.   Load up our database library
  168.  ------------------------------------------------*/
  169.  
  170. $INFO['sql_driver'] = !$INFO['sql_driver'] ? 'mySQL' : $INFO['sql_driver'];
  171.  
  172. $to_require = $root_path."sources/Drivers/".$INFO['sql_driver'].".php";
  173. require ($to_require);
  174.  
  175. $DB = new db_driver;
  176.  
  177. $DB->obj['sql_database']     = $INFO['sql_database'];
  178. $DB->obj['sql_user']         = $INFO['sql_user'];
  179. $DB->obj['sql_pass']         = $INFO['sql_pass'];
  180. $DB->obj['sql_host']         = $INFO['sql_host'];
  181. $DB->obj['sql_tbl_prefix']   = $INFO['sql_tbl_prefix'];
  182.  
  183. // Get a DB connection
  184. $DB->connect();
  185.  
  186. //------------------------------------------------
  187. // Fix up the "show" ID's for the menu tree...
  188. //
  189. // show=1,4,5 holds the current ID's, clicking on a
  190. // collapse link creates out=4 - "4" is then removed
  191. // from the show link.
  192. //
  193. // Good eh?
  194. //------------------------------------------------
  195.  
  196. if ($IN['show'] == 'none')
  197. {
  198.     $IN['show'] = "";
  199. }
  200. else if ($IN['show'] == 'all')
  201. {
  202.     $IN['show']     = "";
  203.     
  204.     foreach($CATS as $cid => $name)
  205.     {
  206.         $IN['show'] .= $cid.',';
  207.     }
  208. }
  209. else
  210. {
  211.     $IN['show'] = preg_replace( "/(?:^|,)".$IN['out']."(?:,|$)/", ",", $IN['show'] );
  212.     $IN['show'] = preg_replace( "/,,/" , "" , $IN['show'] );
  213.     $IN['show'] = preg_replace( "/,$/" , "" , $IN['show'] );
  214.     $IN['show'] = preg_replace( "/^,/" , "" , $IN['show'] );
  215. }
  216.  
  217.  
  218. //------------------------------------------------
  219. // Admin.php Rules:
  220. //
  221. // No adsess number?
  222. // -----------------
  223. //
  224. // Then we log into the admin CP
  225. //
  226. // Got adsess number?
  227. // ------------------
  228. //
  229. // Then we check the cookie "ad_login" for a session key.
  230. //
  231. // If this session key matches the one stored in the admin_sessions
  232. // table, then we check the data against the data stored in the 
  233. // profiles table.
  234. //
  235. // The session key and ad_sess keys are generated each time we log in.
  236. //
  237. // If we don't have a valid adsess in the URL, then we ask for a log in.
  238. //
  239. //------------------------------------------------
  240.  
  241. $session_validated = 0;
  242. $this_session      = array();
  243.  
  244. $validate_login = 0;
  245.  
  246. if ($IN['login'] != 'yes') {
  247.  
  248.     if ( (!$IN['adsess']) or (empty($IN['adsess'])) or (!isset($IN['adsess'])) or ($IN['adsess'] == "") )
  249.     {
  250.         //----------------------------------
  251.         // No URL adsess found, lets log in.
  252.         //----------------------------------
  253.         
  254.         do_login("No administration session found");
  255.     }
  256.     else
  257.     {
  258.         //----------------------------------
  259.         // We have a URL adsess, lets verify...
  260.         //----------------------------------
  261.         
  262.         $DB->query("SELECT * FROM ibf_admin_sessions WHERE ID='".$IN['adsess']."'");
  263.         $row = $DB->fetch_row();
  264.         
  265.         if ($row['ID'] == "")
  266.         {
  267.             //----------------------------------
  268.             // Fail-safe, no DB record found, lets log in..
  269.             //----------------------------------
  270.             
  271.             do_login("Could not retrieve session record");
  272.         }
  273.         else if ($row['MEMBER_ID'] == "")
  274.         {
  275.         
  276.             //----------------------------------
  277.             // No member ID is stored, log in!
  278.             //----------------------------------
  279.             
  280.             do_login("Could not retrieve a valid member id");
  281.             
  282.         }
  283.         else
  284.         {
  285.             //----------------------------------
  286.             // Key is good, check the member details
  287.             //----------------------------------
  288.             
  289.             $DB->query("SELECT * FROM ibf_members WHERE id='".$row['MEMBER_ID']."'");
  290.             $MEMBER = $DB->fetch_row();
  291.             
  292.             if ($MEMBER['id'] == "")
  293.             {
  294.             
  295.                 //----------------------------------
  296.                 // Ut-oh, no such member, log in!
  297.                 //----------------------------------
  298.                 
  299.                 do_login("Member ID invalid");
  300.                 
  301.             }
  302.             else
  303.             {
  304.                 //----------------------------------
  305.                 // Member found, check passy
  306.                 //----------------------------------
  307.                 
  308.                 if ($row['SESSION_KEY'] != $MEMBER['password'])
  309.                 {
  310.                     //----------------------------------
  311.                     // Passys don't match..
  312.                     //----------------------------------
  313.                     
  314.                     do_login("Session member password mismatch");
  315.                     
  316.                 }
  317.                 else
  318.                 {
  319.                     //----------------------------------
  320.                     // Do we have admin access?
  321.                     //----------------------------------
  322.                     
  323.                     $DB->query("SELECT * FROM ibf_groups WHERE g_id='".$MEMBER['mgroup']."'");
  324.                     
  325.                     $GROUP = $DB->fetch_row();
  326.                     
  327.                     if ($GROUP['g_access_cp'] != 1)
  328.                     {
  329.                         do_login("You do not have access to the administrative CP");
  330.                     }
  331.                     else
  332.                     {
  333.                         $session_validated = 1;
  334.                         $this_session      = $row;
  335.                     }
  336.                 }
  337.             }
  338.         }
  339.     }
  340. }
  341. else 
  342. {
  343.     //----------------------------------
  344.     // We must have submitted the form
  345.     // time to check some details.
  346.     //----------------------------------
  347.     
  348.     if ( empty($IN['username']) )
  349.     {
  350.         do_login("You must enter a username before proceeding");
  351.     }
  352.     
  353.     if ( empty($IN['password']) )
  354.     {
  355.         do_login("You must enter a password before proceeding");
  356.     }
  357.     
  358.     //----------------------------------
  359.     // Attempt to get the details from the
  360.     // DB
  361.     //----------------------------------
  362.     
  363.     $DB->query("SELECT name, password, id, mgroup FROM ibf_members WHERE LOWER(name)='".strtolower($IN['username'])."'");
  364.     $mem = $DB->fetch_row();
  365.     
  366.     if ( empty($mem['id']) )
  367.     {
  368.         do_login("Could not find a record matching that username, please check the spelling");
  369.     }
  370.     
  371.     $pass    = md5( $IN['password'] );
  372.     
  373.     if ($pass != $mem['password'])
  374.     {
  375.         do_login("The password entered did not match the one in our records");
  376.     }
  377.     else
  378.     {
  379.         $DB->query("SELECT * FROM ibf_groups WHERE g_id='".$mem['mgroup']."'");
  380.                     
  381.         $GROUP = $DB->fetch_row();
  382.         
  383.         if ($GROUP['g_access_cp'] != 1)
  384.         {
  385.             do_login("You do not have access to the administrative CP");
  386.         }
  387.         else
  388.         {
  389.         
  390.             //----------------------------------
  391.             // All is good, rejoice as we set a
  392.             // session for this user
  393.             //----------------------------------
  394.             
  395.             $sess_id = md5( uniqid( microtime() ) );
  396.             
  397.             $db_string = $DB->compile_db_insert_string( array (
  398.                                                                 'ID'           => $sess_id,
  399.                                                                 'IP_ADDRESS'   => $IN['IP_ADDRESS'],
  400.                                                                 'MEMBER_NAME'  => $mem['name'],
  401.                                                                 'MEMBER_ID'    => $mem['id'],
  402.                                                                 'SESSION_KEY'  => $pass,
  403.                                                                 'LOCATION'     => 'index',
  404.                                                                 'LOG_IN_TIME'  => time(),
  405.                                                                 'RUNNING_TIME' => time(),
  406.                                                       )        );
  407.                                                       
  408.             $DB->query("INSERT INTO ibf_admin_sessions (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");
  409.         
  410.             $IN['AD_SESS'] = $sess_id;
  411.             
  412.             // Print the "well done page"
  413.             
  414.             $ADMIN->page_title = "Log in successful";
  415.             
  416.             $ADMIN->page_detail = "You may now proceed to the administrative control panel";
  417.             
  418.             $ADMIN->html .= $SKIN->start_table("Proceed");
  419.             
  420.             $ADMIN->html .= "<tr><td id='tdrow1'><a href='".$INFO['board_url']."/admin.".$INFO['php_ext']."?adsess=".$IN['AD_SESS']."'>Take me to administrative control panel</a></td></tr>";
  421.             
  422.             $ADMIN->html .= $SKIN->end_table();
  423.             
  424.             $ADMIN->output();
  425.         
  426.         }
  427.         
  428.     }
  429.         
  430. }
  431.  
  432.  
  433. //----------------------------------
  434. // Ok, so far so good. If we have a 
  435. // validate session, check the running
  436. // time. if it's older than 2 hours,
  437. // ask for a log in
  438. //----------------------------------
  439.  
  440.  
  441. if ($session_validated == 1)
  442. {
  443.     if ($this_session['RUNNING_TIME'] < ( time() - 60*60*2) )
  444.     {
  445.         $session_validated = 0;
  446.         do_login("This administration session has expired");
  447.     }
  448.     
  449.     //------------------------------
  450.     // Are we checking IP's?
  451.     //------------------------------
  452.     
  453.     else if ($check_ip == 1)
  454.     {
  455.         if ($this_session['IP_ADDRESS'] != $IN['IP_ADDRESS'])
  456.         {
  457.             $session_validated = 0;
  458.             do_login("Your current IP address does not match the one in our records");
  459.         }
  460.     }
  461. }
  462.  
  463. if ($session_validated == 1 )
  464. {
  465.     //------------------------------
  466.     // If we get this far, we're good to go..
  467.     //------------------------------
  468.     
  469.     $IN['AD_SESS'] = $IN['adsess'];
  470.     
  471.     //------------------------------
  472.     // Lets update the sessions table:
  473.     //------------------------------
  474.     
  475.     $DB->query("UPDATE ibf_admin_sessions SET RUNNING_TIME='".time()."', LOCATION='".$IN['act']."' WHERE MEMBER_ID='".$MEMBER['id']."' AND ID='".$IN['AD_SESS']."'");
  476.     
  477.     do_admin_stuff();
  478.     
  479. }
  480. else
  481. {
  482.     //------------------------------
  483.     // Session is not validated...
  484.     //------------------------------
  485.     
  486.     do_login("Session not validated - please attempt to log in again");
  487.     
  488. }
  489.  
  490.  
  491.  
  492. function do_login($message="") {
  493.     global $IN, $DB, $ADMIN, $SKIN;
  494.     
  495.     //-------------------------------------------------------
  496.     // Remove all out of date sessions, like a good boy. Woof.
  497.     //-------------------------------------------------------
  498.     
  499.     $cut_off_stamp = time() - 60*60*2;
  500.     
  501.     $DB->query("DELETE FROM ibf_admin_sessions WHERE RUNNING_TIME < $cut_off_stamp");
  502.     
  503.     //+------------------------------------------------------
  504.     
  505.     $ADMIN->page_detail = "You must have administrative access to successfully log into the Invision Board Admin CP.<br>Please enter your forums username and password below";
  506.     
  507.     if ($message != "")
  508.     {
  509.         $ADMIN->page_detail .= "<br><br><span style='color:red;font-weight:bold'>$message</span>";
  510.     }
  511.     
  512.     $ADMIN->html .= "<script language='javascript'>
  513.                       <!--
  514.                           if (top.location != self.location) { top.location = self.location }
  515.                       //-->
  516.                      </script>
  517.                      ";
  518.     
  519.     $ADMIN->html .= $SKIN->start_form( array( 1 => array('login', 'yes') ) );
  520.     
  521.     $SKIN->td_header[] = array( " "  , "40%" );
  522.     $SKIN->td_header[] = array( " "  , "60%" );
  523.     
  524.     $ADMIN->html .= $SKIN->start_table( "Verification Required" );
  525.     
  526.     $ADMIN->html .= $SKIN->add_td_row( array( "Your Forums Username:",
  527.                                               "<input type='text' style='width:100%' name='username' value=''>",
  528.                                      )      );
  529.         
  530.     $ADMIN->html .= $SKIN->add_td_row( array( "Your Forums Password:",
  531.                                               "<input type='password' style='width:100%' name='password' value=''>",
  532.                                      )      );
  533.                                      
  534.     $ADMIN->html .= $SKIN->end_form("Log in");
  535.     
  536.     $ADMIN->html .= $SKIN->end_table();
  537.         
  538.     $ADMIN->output();
  539.  
  540. }
  541.  
  542.  
  543.  
  544.  
  545.  
  546. function do_admin_stuff() {
  547.     global $IN, $root_path, $INFO, $DB, $SKIN, $ADMIN, $std, $MEMBER, $GROUP, $ibforums;
  548.  
  549.  
  550.     /*----------------------------------
  551.       What do you want to require today?
  552.     ------------------------------------*/
  553.     
  554.     $choice = array(
  555.                      "idx"      => "doframes",
  556.                      "menu"     => "menu",
  557.                      "index"    => "index",
  558.                      "cat"      => "categories",
  559.                      "forum"    => "forums",
  560.                      "mem"      => "member",
  561.                      'group'    => "groups",
  562.                      'mod'      => 'moderator',
  563.                      'op'       => 'settings',
  564.                      'help'     => 'help',
  565.                      'skin'     => 'skins',
  566.                      'wrap'     => 'wrappers',
  567.                      'style'    => 'stylesheets',
  568.                      'image'    => 'imagemacros',
  569.                      'sets'     => 'stylesets',
  570.                      'templ'    => 'templates',
  571.                      'lang'     => 'languages',
  572.                      'import'   => 'skin_import',
  573.                      'modlog'   => 'modlogs',
  574.                    );
  575.     
  576.                     
  577.     /***************************************************/
  578.     
  579.     $IN['act'] = $IN['act'] == '' ? "idx" : $IN['act'];
  580.     
  581.     // Check to make sure the array key exits..
  582.     if (! isset($choice[$IN['act']]) ) {
  583.         $IN['act'] = 'idx';
  584.     }
  585.     
  586.     // Require and run
  587.     
  588.     if ($IN['act'] == 'idx')
  589.     {
  590.         print $SKIN->frame_set();
  591.         exit;
  592.     }
  593.     else if ($IN['act'] == 'menu')
  594.     {
  595.         $ADMIN->menu();
  596.     }
  597.     else
  598.     {
  599.         require $root_path."sources/Admin/ad_".$choice[$IN['act']].".php";
  600.     }
  601.     
  602. }
  603.  
  604.  
  605.  
  606. //+-------------------------------------------------
  607. // GLOBAL ROUTINES
  608. //+-------------------------------------------------
  609.  
  610. function fatal_error($message="", $help="") {
  611.     echo("$message<br><br>$help");
  612.     exit;
  613. }
  614. ?>
  615.